<template>
  <div class="login">
    <div class="box">
      <h2>登录</h2>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <el-form-item prop="username">
          <el-input :prefix-icon="User" v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" show-password v-model="ruleForm.password" />
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%;" type="primary" @click="submitForm(ruleFormRef)">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import {User,Lock} from "@element-plus/icons-vue"
import {loginApi} from "@/api/api"
import {useRouter} from "vue-router"
import { ElMessage } from "element-plus";
import "element-plus/es/components/message/style/css";
interface RuleForm {
  username: string;
  password: string;
}
const router = useRouter()
const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  username: "demo",
  password: "zh@hm#23",
});

const rules = reactive<FormRules<RuleForm>>({
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      loginApi(ruleForm).then((res:any)=>{
        console.log(res);
        if(res.code==10000){
          ElMessage.success("登录成功");
          localStorage.setItem("token",res.data.token)
          router.push("/admin")
        }
      })
    } else {
      console.log("error submit!", fields);
    }
  });
};

</script>

<style lang="scss" scoped>
.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: pink;
  .box {
    width: 500px;
    padding: 50px;
    box-sizing: border-box;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0 10px 5px #eee;
    h2 {
      text-align: center;
      margin-bottom: 20px;
    }
  }
}
</style>
